#!/usr/bin/env python3
#-*- coding utf-8 -*-
#--------------------
#@author  : xiaowu
#@Time    : 2020/1/18 22:39
#@File    : 03inserttable.py
# -------------------------

import pymysql
import datetime

def create_table():
    # 打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "test")

    # 使用cursor()方法创建一个游标对象cursor
    cursor = db.cursor()

    # SQL插入语句
    sql = 'insert into EEQA(FIRST_NAME,LAST_NAME,AGE,SEX,INCOME) values(%s,%s,%s,%s,%s);'
    data = [
        ('july', 'zhangsan','21','M','20152'),
        ('june', 'lisi','27','M','24162'),
        ('marin', 'wangwu','24','M','24563')
    ]

    try:
        cursor.executemany(sql,data)
        # 提交到数据库执行
        db.commit()
        print("insert table success")
    except Exception as e:
        print("create table failed,case %s" %e)
        # 如果发生错误就回滚
        db.rollback()
    finally:
        db.close()

def main():
    create_table()


if __name__ == '__main__':
    main()

